import numpy as np 
import time, math
from langchain.tools import StructuredTool

class BankCustomerMockInentTool:
    def __init__(self):
        np.random.seed(math.floor(time.time()))
        self.intent_history = []
        self.initial_intents = [
            "查询账户余额",
            "挂失账户",
            "信用卡问答"
        ]
        
        self.intent_transition = {}
        self.intent_transition["查询账户余额"] = [
            "挂失账户",
            "信用卡问答"
        ]
        self.intent_transition["挂失账户"] = [
            "信用卡问答"
        ]
        self.intent_transition["信用卡问答"] = [
            "查询账户余额",
            "挂失账户"
        ]
    
        self.credit_card_questions = [
            "可以使用美元还款吗？",
            "高端卡有什么额外的权益?"
        ]
        
        self.finish_intent = "没有其他需要了，请直接输出FINISH指令结束对话。"
    

    def provide_my_intent(self) -> str:
        """
        获得我的意图的函数
        """
        
        if len(self.intent_history) == 0:
            intent = np.random.choice(self.initial_intents)
            if intent == "信用卡问答":
                intent = intent + "#" + np.random.choice(self.credit_card_questions)
            self.intent_history.append(intent)
            return f"客户意图：{intent}"
        
        if np.random.rand() > 1.0 / (1 + len(self.intent_history)): 
            return f"客户意图：{self.finish_intent}"
        
        last_intent = self.intent_history[-1]
        if last_intent.find("#") >= 0:
            last_intent = last_intent.split("#")[0]
        intents = self.intent_transition.get(last_intent, self.initial_intents)
        
        tryN = 0
        while True:
            intent = np.random.choice(intents)
            if intent == "信用卡问答":
                intent = intent + "#" + np.random.choice(self.credit_card_questions)
            if not intent in self.intent_history:
                break
            
            tryN += 1
            if tryN > 10:
                intent = self.finish_intent
                break
        if intent != self.finish_intent:
            self.intent_history.append(intent)
        return f"客户意图：{intent}"
    
    def as_tool(self):
        return StructuredTool.from_function(
            func=self.provide_my_intent,
            name="ProvideMyIntentTool",
            description="获得我的提问意图的工具",
        )

    
def provide_my_info() -> str:
    """
    获得我的信息的函数
    """
    
    customer_info = {
        "姓名": "张三",
        "身份证号": "123456789012345678",
        "手机号": "13800138000",
        "地址": "北京市朝阳区某某小区"
    }
    
    info = f"客户信息：姓名={customer_info['姓名']}, 身份证号={customer_info['身份证号']}, 手机号={customer_info['手机号']}, 地址={customer_info['地址']}"
    
    return info
    
def provide_my_verify_code() -> str:
    """
    获得银行发送给我的手机验证码的函数
    """
    verify_code = ""
    for i in range(4):
        verify_code += str(np.random.randint(0, 10))
    
    return f"验证码：{verify_code}"

def provide_process_account_type() -> str:
    '''
    获得处理账户类型的函数
    '''
    account_types = [
        "储蓄账户",
        "信用卡账户"
    ]
    
    np.random.shuffle(account_types)
    return f"账户类型：{account_types[0]}"

if __name__ == '__main__':
    intent_mocker = BankCustomerMockInentTool()
    for i in range(10):
        intent = intent_mocker.provide_my_intent()
        print(intent)
        if intent.find("没有了") >= 0:
            break
    